package cn.ekuma.epos.datalogic.define.dao;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

import cn.ekuma.data.dao.BaseDAO;
import cn.ekuma.epos.symmetricds.AppConfig;
import cn.ekuma.epos.symmetricds.bean.TriggerRouter;

public class TriggerRouterDAO extends BaseDAO<TriggerRouter> {

	public TriggerRouterDAO(I_Session s) {
		super(s);
	}

	@Override
	public TriggerRouter readValues(DataRead dr,TriggerRouter obj) throws BasicException {
		if(obj==null)
			obj=new TriggerRouter();
		obj.setTrigerId(dr.getString(1));
		obj.setRouterId(dr.getString(2));
		obj.setInitialLoadOrder(dr.getInt(3));
		obj.setInitialLoadSelect(dr.getString(4));
		obj.setPingBackEnabled(dr.getBoolean(5));
		obj.setCreateTime(dr.getTimestamp(6));
		obj.setLastUpdateBy(dr.getString(7));
		obj.setLastUpdateTime(dr.getTimestamp(8));
		return obj;
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s,
				AppConfig.getAppProperty(AppConfig.TABLE_PREFIX)+"_"+"trigger_router",
                new Field[]{
                   new Field("trigger_id",Datas.STRING, Formats.STRING),
                   new Field("router_id",Datas.STRING, Formats.STRING),
                   new Field("initial_load_order",Datas.INT, Formats.INT),
                   new Field("initial_load_select",Datas.STRING, Formats.STRING),
                   new Field("ping_back_enabled",Datas.BOOLEAN, Formats.BOOLEAN),
                   new Field("create_time",Datas.TIMESTAMP, Formats.TIMESTAMP),
                   new Field("last_update_by",Datas.STRING, Formats.STRING),
                   new Field("last_update_time",Datas.TIMESTAMP, Formats.TIMESTAMP)
                 },
                new int[]{0,1}
                );
	}
	
	/**
	 *  <table name="trigger_router" description="Map a trigger to a router.">
        <column name="trigger_id" type="VARCHAR" size="50" required="true" primaryKey="true"  description="The id of a trigger." />
        <column name="router_id" type="VARCHAR" size="50" required="true" primaryKey="true"  description="The id of a router." />
        <column name="initial_load_order" type="INTEGER" required="true" default="1"  description="Order sequence of this table when an initial load is sent to a node. " />
        <column name="initial_load_select" type="LONGVARCHAR"  description="Optional expression that can be used to pair down the data selected from a table during the initial load process." />
        <column name="ping_back_enabled" type="BOOLEANINT" size="1" required="true" default="0" description="When enabled, the node will route data that originated from a node back to that node.  This attribute is only effective if sync_on_incoming_batch is set to 1."/> 
        <column name="create_time" type="TIMESTAMP" required="true"  description="Timestamp when this entry was created." />
        <column name="last_update_by" type="VARCHAR" size="50"  description="The user who last updated this entry." />
        <column name="last_update_time" type="TIMESTAMP" required="true"  description="Timestamp when a user last updated this entry." />
    </table>
	 */

	@Override
	public void writeInsertValues(DataWrite dp, TriggerRouter obj)
			throws BasicException {
		
		dp.setString(1,obj.getTrigerId());
		dp.setString(2,obj.getRouterId());
		dp.setInt(3,obj.getInitialLoadOrder());
		dp.setString(4,obj.getInitialLoadSelect());
		dp.setBoolean(5,obj.isPingBackEnabled());
		dp.setTimestamp(6,obj.getCreateTime());
		dp.setString(7,obj.getLastUpdateBy());
		dp.setTimestamp(8,obj.getLastUpdateTime());
		
	}

	@Override
	public Class getSuportClass() {
		return TriggerRouter.class;
	}

}
